package com.yjhb.android.sbz.mvp.mytask;

import com.yjhb.android.sbz.entity.MyTask;
import com.yjhb.android.sbz.http.HttpObserver;
import com.yjhb.android.sbz.http.HttpTool;
import com.yjhb.android.sbz.mvp.ConventionPresenter;

import java.util.List;

/**
 * 创建者 zx
 * 创建时间 2020/12/17 10:55
 */
public class MyTaskListPresenter extends ConventionPresenter<MyTaskListView> {
    private final String TAG = "MyTaskListPresenter";

    public MyTaskListPresenter(MyTaskListView myTaskListView) {
        super(myTaskListView);
    }

    @Override
    protected void onDestroy() {

    }

    public void cancelTask(Integer fetchId) {
        if (view != null) {
            view.onWait("正在取消报名");
        }
        HttpTool.request(model.cancelTask(fetchId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess("报名已取消");
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });

    }


    /**
     * 获取我的  已提交的任务  待审核、
     *
     * @param state
     * @param size
     * @param current
     */
    public void getOrderList(int state, int size, final int current) {
        if (model!=null) {
            HttpTool.request(model.getOrderList(state, size, current), new HttpObserver<List<MyTask>>() {
                @Override
                public void onResult(List<MyTask> data) {
                    if (view != null) {
                        if (current == 1) {
                            view.onRefresh(data);
                        } else {
                            view.onLoad(data);
                        }
                    }
                }

                @Override
                public void onError(int code, String msg) {
                    if (view != null) {
                        view.onFailure(code, msg);
                    }
                }
            });
        }
    }


    /**
     * 重新报名
     *
     * @param taskId
     */
    public void fetch(String taskId) {
        if (view != null) {
            view.onWait("正在重新报名");
        }
        HttpTool.request(model.fetch(taskId), new HttpObserver<String>() {
            @Override
            public void onResult(String data) {
                if (view != null) {
                    view.onSuccess("报名成功，请在进行中任务查看");
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }


    /**
     * 删除超时订单
     * @param orderId
     */
    public void deleteOrder(Integer orderId) {
        if (view != null) {
            view.onWait("正在删除...");
        }
        HttpTool.request(model.deleteOrder(orderId), new HttpObserver<Object>() {
            @Override
            public void onResult(Object data) {
                if (view != null) {
                    view.onSuccess("删除成功！");
                }
            }

            @Override
            public void onError(int code, String msg) {
                if (view != null) {
                    view.onFailure(code, msg);
                }
            }
        });
    }

}
